﻿#include "CClientViewModule.h"
#include "../game/MsgID.h"
#include "../game/MsgStruct.h"
#include "../game/MiscDefine.h"

#include <string.h>
#include <stdio.h>
#include <Windows.h>

#pragma warning(disable:4996)

int hide_ready_btn();
extern int refresh_all_atoms_tmp(ST_NOTIFY_VIEW_ATOMS_CHANGED* c);

static char _log_buf[1024] = {0};

#define CLINET_DEBUG_LOG(fmt, ...) { \
	memset(_log_buf, 0, sizeof(_log_buf)); \
	sprintf(_log_buf, "%s:%d fn:%s " fmt "\n", strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__); \
	WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), _log_buf, strlen(_log_buf), NULL, NULL); \
}

static int ON_MODULE_MSG(CClientViewModule, MSG_ID_SET_BTN) {
	ST_SET_BTN* cmd = (ST_SET_BTN*)msg;
	if (CMD_TYPE_BTN_HIDE == cmd->cmd_type) {
		if (IDB_START_GAME == cmd->btn_id) {
			CLINET_DEBUG_LOG("hide_ready_btn ");
			hide_ready_btn();
		}
	}
	return 0;
}

static int ON_MODULE_MSG(CClientViewModule, MSG_ID_NOTIFY_VIEW_ATOMS_CHANGED) {
	ST_NOTIFY_VIEW_ATOMS_CHANGED* c = (ST_NOTIFY_VIEW_ATOMS_CHANGED*)msg;
	CLINET_DEBUG_LOG(" atoms count=%d", c->atoms_count);
	refresh_all_atoms_tmp(c);
	return 0;
}

static int cclient_view_reg_func_on_msgid(CModule* thes) {
	REG_MODULE_MSG_FUNC(CClientViewModule, thes, MSG_ID_SET_BTN);
	REG_MODULE_MSG_FUNC(CClientViewModule, thes, MSG_ID_NOTIFY_VIEW_ATOMS_CHANGED);
    return 0;
}

int construct_client_view(CClientViewModule* thes, const char* module_name) {
	construct_module(&thes->parent, module_name);
	cclient_view_reg_func_on_msgid(AS_MODULE(thes));
	AS_MODULE(thes)->run(AS_MODULE(thes), USE_DEFAULT_MODULE_ENTRY);
	return 0;
}

